package com.wtao.mediaprojection;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    String TAG = "MainActivity";
    //权限请求码
    private final static int PERMISSION_REQUEST_CODE = 1101;
    //开始按钮，停止按钮
    Button btn_start_recorder;
    Button btn_stop_recorder;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化按钮
        btn_start_recorder = findViewById(R.id.btn_start_recorder);
        btn_stop_recorder = findViewById(R.id.btn_stop_recorder);
        //点击按钮，请求录屏
        btn_start_recorder.setOnClickListener(this);
        btn_stop_recorder.setOnClickListener(this);

        //点击视频录制之前，需要把权限都申请下来才不会报错
        if (Build.VERSION.SDK_INT >= 23) {
            if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) &&
                    (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
                Log.e(TAG, "权限申请 WRITE_EXTERNAL_STORAGE RECORD_AUDIO");
                ActivityCompat.requestPermissions(this, new String[]{
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE}, 12);
            }
        }
    }


    //没有权限，去请求权限后，需要判断用户是否同意权限请求
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQUEST_CODE) {
            //请求码相同
            /*if (grantResults.length > 0 &&
                    (grantResults[0] != PackageManager.PERMISSION_GRANTED ||
                            grantResults[1] != PackageManager.PERMISSION_GRANTED ||
                            grantResults[2] != PackageManager.PERMISSION_GRANTED)) {
                //如果结果都存在，但是至少一个没请求成功，弹出提示
                Toast.makeText(MainActivity.this, "请同意必须的应用权限，否则无法正常使用该功能！", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                    grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
                //如果结果都存在，两个权限都申请成功，连接服务，启动录屏
                Toast.makeText(MainActivity.this, "权限申请成功，用户同意！", Toast.LENGTH_SHORT).show();

            }*/
        }
    }


    @Override
    //返回方法，获取返回的信息
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "onActivityResult");
        //首先判断请求码是否一致，结果是否ok
        if (requestCode == MediaProjectionHelper.REQUEST_CODE && resultCode == RESULT_OK) {
            //录屏请求成功，使用工具MediaProjection录屏
            //从发送获得的数据和结果中获取该工具
            Log.e(TAG, "onActivityResult-->startRecord");
            MediaProjectionHelper.getInstance().startRecord(resultCode, data);
        } else {
            Log.e(TAG, "取消了");
        }
    }

    @Override
    //点击事件
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start_recorder:
                //点击请求录屏后，第一件事，检查权限
                if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
                        && (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)
                        && (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.FOREGROUND_SERVICE) != PackageManager.PERMISSION_GRANTED)) {
                    Log.e(TAG, "没有权限");
                    return;
                }
                //参数传过去以后，如果在录制，提示
                MediaProjectionHelper.getInstance().setConfig(this);
                break;
            case R.id.btn_stop_recorder:
                MediaProjectionHelper.getInstance().stopRecord();
                break;
        }
    }


    //当应用结束的时候，需要解除绑定服务，防止造成内存泄漏
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //unbindService(serviceConnection);
        MediaProjectionHelper.getInstance().unBindService();
    }
}